<?php

namespace app\admin\model;

use think\Model;


class Coupon extends Model
{

    

    

    // 表名
    protected $table = 'coupon';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'discount_type_text',
        'goods_type_text',
        'sent_type_text',
        'use_type_text',
        'starttime_text',
        'endtime_text'
    ];
    

    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }

    
    public function getDiscountTypeList()
    {
        return ['1' => __('Discount_type 1'), '2' => __('Discount_type 2')];
    }

    public function getGoodsTypeList()
    {
        return ['all' => __('Goods_type all'), 'part' => __('Goods_type part')];
    }

    public function getSentTypeList()
    {
        return ['send' => __('Sent_type send'), 'draw' => __('Sent_type draw')];
    }

    public function getUseTypeList()
    {
        return ['mini' => __('Use_type mini')];
        return ['mini' => __('Use_type mini'), 'pc' => __('Use_type pc')];
    }


    public function getDiscountTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['discount_type']) ? $data['discount_type'] : '');
        $list = $this->getDiscountTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getGoodsTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['goods_type']) ? $data['goods_type'] : '');
        $list = $this->getGoodsTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSentTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sent_type']) ? $data['sent_type'] : '');
        $list = $this->getSentTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getUseTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['use_type']) ? $data['use_type'] : '');
        $valueArr = explode(',', $value);
        $list = $this->getUseTypeList();
        return implode(',', array_intersect_key($list, array_flip($valueArr)));
    }


    public function getStarttimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['starttime']) ? $data['starttime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setUseTypeAttr($value)
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    protected function setStarttimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


}
